home *** CD-ROM | disk | FTP | other *** search
/ A.C.E. 2 / ACE CD 2.iso / FILES / UTILS / AMOSPRO4.DMS / in.adf / Tutorials / Procedures_2.AMOS / Procedures_2.amosSourceCode
Encoding:
AMOS Source Code  |  1992-09-28  |  7.5 KB  |  254 lines

  1. '*************************** 
  2. '*    AMOS Professional    *             INSTRUCTIONS COVERED
  3. '*                         * 
  4. '*       PROCEDURES 2      *                  Global 
  5. '*                         *                  Shared 
  6. '* (c) Europress Software  *                  Param
  7. '*                         *                  Param$ 
  8. '*     Ronnie Simpson      *                  Param# 
  9. '***************************                  Pop Proc 
  10. '                                             On Proc
  11. '                                             On Break Proc  
  12. '
  13. '------------------------------------------- 
  14. 'GENERAL 
  15. '------------------------------------------- 
  16. 'In this the second of the procedure tutorials we will explore the ways
  17. 'in which information can be passed between procedures and the rest of 
  18. 'the program.
  19. '------------------------------------------- 
  20. 'GLOBAL AND SHARED VARIABLES 
  21. '------------------------------------------- 
  22. 'Global variables are declared from the main program before the procedure
  23. 'is called. (this only has to be done once)
  24. '
  25. 'eg.          Global A,B,C$,D()
  26. '
  27. 'When a variable or list of variables is declared global then they can be  
  28. 'accessed from anywhere in the program. (both inside and outside procedures) 
  29. 'This provides an easy method of transferring the information between any
  30. 'number of procedures
  31. '
  32. 'A second method of passing information is to declare the variable shared. 
  33. '
  34. 'eg.          Shared A,B,C$,D()
  35. '
  36. 'This instruction is placed inside the procedure and the first time the
  37. 'procedure is called then the listed variables will be treated as global.
  38. 'Any arrays to be declared in this way must first have been dimensioned
  39. 'in the main part of the program.
  40. '------------------------------------------- 
  41. 'PASSING PARAMETERS
  42. '------------------------------------------- 
  43. 'When calling a procedure it is possible to pass parameters. 
  44. '
  45. 'eg.           Proc MULTIPLY[A,B]  
  46. '
  47. 'The parameters are placed inside square brackets after the procedure name 
  48. 'and these should be matched with a similar set in the procedure definitions.
  49. '
  50. 'eg.           A=100 : B=$"AMOS "  
  51. '              Proc MULTIPLY[A,B$]   
  52. '                 :       :  
  53. '              Procedure MULTIPLY[X,Y$]
  54. '              Print X*2 
  55. '              Print Y$+Y$ 
  56. '              End Proc
  57. '
  58. 'A couple of things to note from the above example is that:- 
  59. '    (1)The number and type of parameters must match with those in the 
  60. '       procedure definition.
  61. '    (2)The variables used in the procedure definition can be
  62. '       local to the procedure.
  63. '
  64. 'It is also possible to return a variable or string from the procedure 
  65. 'to the main program using a similar method as above but placing the 
  66. 'square brackets after the End Proc
  67. '
  68. 'eg.           A=100 : B$="AMOS "
  69. '              Proc MULTIPLY[A,B$] 
  70. '              Print Param 
  71. '                 :       :  
  72. '              Procedure MULTIPLY[X,Y$]
  73. '              X=X*2 
  74. '              Print Y$+Y$ 
  75. '              End Proc[X] 
  76. '
  77. 'Please note that only one parameter can be returned in this way and 
  78. 'depending on the type of variable it is the result will be held in
  79. 'one of the following functions:-
  80. '
  81. '              
  82. '              Param   (an integer)
  83. '              Param$  (a string)
  84. '              Param#  (a real number or floating point) 
  85. '
  86. '------------------------------------------- ' 
  87. 'POP PROC
  88. '------------------------------------------- 
  89. 'Normally once a procedure has been called, control will only be returned to 
  90. 'the main program when the End Proc is reached, but if a hasty return is 
  91. 'required the Pop Proc instruction will immediately abort the procedure and
  92. 'return control to the instruction following the call. 
  93. '
  94. 'eg.           Proc QUIT 
  95. '              Edit  
  96. '
  97. '              Procedure QUIT
  98. '              For X=1 To 5000 
  99. '              If X=1000 Then Pop Proc 
  100. '              Next  
  101. '              Print "I'll never get printed"
  102. '              End Proc
  103. '
  104. '------------------------------------------- 
  105. 'On Break Proc 
  106. '------------------------------------------- 
  107. 'A jump can also be made to a procedure whenever the program is interupted 
  108. 'by using the On Break Proc instruction:-
  109. '
  110. '              On Break Proc GOODBYE 
  111. '              Do  
  112. '              Loop  
  113. '              Procedure GOODBYE 
  114. '              Print "bye for now" : Wait 100 : Edit 
  115. '              End Proc
  116. '
  117. '------------------------------------------- 
  118. 'WORKING EXAMPLE 
  119. '------------------------------------------- 
  120. 'I am sure with a little effort you will be able to use your new found 
  121. 'programming skills to turn this program into a rival of D/Paint IV.   
  122. '------------------------------------------- 
  123. '
  124. Dim M$(5)
  125. Global M$(),M
  126. 'Rem *** open hires screen and set palette 
  127. '
  128. Screen Open 0,640,200,16,Hires : Flash Off : Change Mouse 2
  129. Palette $0,$F00,$F0,$F,$FF0,$F0F,$FF,$F70,$7F,$70F,$F07,$333,$666,$999,$CCC,$FFF
  130. Curs Off : Cls 0 : Paper 11 : Pen 14
  131. '
  132. '
  133. Rem *** reserve some memory for the screen zones 
  134. '
  135. Reserve Zone 21
  136. '
  137. '
  138. Rem *** make the variable C global (procedures use this as the ink colour) 
  139. Global C
  140. '
  141. '
  142. Rem *** start the main program by calling the INIT procedure 
  143. '
  144. INIT
  145. '
  146. '
  147. Do 
  148.    Clip 
  149.    AWAIT
  150.    Exit If Param=5
  151.    If Param>5
  152.       Proc CHANGECOLOUR[Param-6]
  153.    Else On M Proc PHILL,LINE,SIRCLE,RECTANGLE
  154.    End If 
  155. Loop 
  156. '
  157. '
  158. Edit 
  159. '
  160. '
  161. '
  162. Rem *** the various procedures 
  163. '
  164. Procedure INIT
  165.    M$(1)="Fill     "
  166.    M$(2)="Line     "
  167.    M$(3)="Circle   "
  168.    M$(4)="Rectangle"
  169.    M$(5)="Quit     "
  170.    M=3
  171.    Locate 73,1 : Print Border$(Zone$(" FILL ",1),2)
  172.    Locate 73,4 : Print Border$(Zone$(" LINE ",2),2)
  173.    Locate 73,7 : Print Border$(Zone$("CIRCLE",3),2)
  174.    Locate 73,10 : Print Border$(Zone$(" RECT.",4),2)
  175.    Locate 73,13 : Print Border$(Zone$(" QUIT ",5),2)
  176.    X=590 : Y=128 : Set Paint 1
  177.    For I=0 To 7
  178.       Ink I,,8
  179.       Bar X,Y To X+16,Y+8 : Set Zone I+6,X,Y To X+16,Y+8
  180.       Add Y,9
  181.    Next 
  182.    X=610 : Y=128
  183.    For I=8 To 15
  184.       Ink I,,3
  185.       Bar X,Y To X+16,Y+8 : Set Zone I+6,X,Y To X+16,Y+8
  186.       Add Y,9
  187.    Next 
  188.    Ink 1,,15 : Bar 576,121 To 639,126
  189.    Ink 15 : Box 0,0 To 570,188
  190.    Ink 11,,14
  191.    Bar 0,190 To 570,200
  192.    C=1
  193. End Proc
  194. Procedure AWAIT
  195.    Pen 14 : Paper 11
  196.    Locate 1,24 : Print " Current Mode-";M$(M);
  197.    Wait 10
  198.    Do 
  199.       X=Mouse Zone
  200.       If X>0 and X<5 and Mouse Key Then M=X : Locate 1,24 : Print " Current Mode-";M$(M);
  201.       Exit If X=0 and Mouse Key
  202.       Exit If X>4 and Mouse Key
  203.    Loop 
  204. End Proc[X]
  205. Procedure CHANGECOLOUR[X]
  206.    C=X
  207.    Pen C : Locate 60,24 : Print "COLOUR>>>>";
  208.    Ink C,,15 : Bar 576,121 To 639,126
  209.    Wait 20
  210. End Proc
  211. Procedure LINE
  212.    Shared X,Y
  213.    Gr Writing 2
  214.    POSITION
  215.    While Mouse Key
  216.       X2=X Screen(X Mouse) : Y2=Y Screen(Y Mouse)
  217.       Draw X,Y To X2,Y2 : Draw X,Y To X2,Y2
  218.    Wend 
  219.    Gr Writing 0 : Ink C : Draw X,Y To X2,Y2
  220. End Proc
  221. Procedure SIRCLE
  222.    Shared X,Y
  223.    Gr Writing 2
  224.    POSITION
  225.    While Mouse Key
  226.       X2=X Screen(X Mouse) : Y2=Y Screen(Y Mouse)
  227.       R1=Abs(X-X2)/2 : R2=Abs(Y-Y2) : R=Max(R1,R2) : If R<1 Then R=1
  228.       Circle X,Y,R : Circle X,Y,R
  229.    Wend 
  230.    Gr Writing 0 : Ink C : Circle X,Y,R
  231. End Proc
  232. Procedure RECTANGLE
  233.    Shared X,Y
  234.    Gr Writing 2
  235.    POSITION
  236.    While Mouse Key
  237.       X2=X Screen(X Mouse) : Y2=Y Screen(Y Mouse)
  238.       Box X,Y To X2,Y2 : Box X,Y To X2,Y2
  239.    Wend 
  240.    Gr Writing 0 : Ink C : Box X,Y To X2,Y2
  241. End Proc
  242. Procedure PHILL
  243.    Repeat 
  244.       X=X Screen(X Mouse) : Y=Y Screen(Y Mouse)
  245.    Until X>0 and X<570 and Y>0 and Y<188 and Mouse Key
  246.    Ink C : Paint X,Y
  247. End Proc
  248. Procedure POSITION
  249.    Shared X,Y
  250.    Repeat 
  251.       X=X Screen(X Mouse) : Y=Y Screen(Y Mouse)
  252.    Until Mouse Key
  253.    Clip 1,1 To 569,187
  254. End Proc